using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using RFS.BizLogic.PaiShi.Ports;
using RFS.BizLogic.Sys.impl;
using RFS.Domain.PaiShi.Zuhuo;
using RFS.Domain.Sys;
using ycx;

namespace ItProto1
{
    public class ItZahuaRepo
    {
        [Test]
        public void T1_zuhua_Crid_correct()
        {
            // var en = AntiObject.Ins.Get<IRepoZahua>();
            var rzu = AntiObject.Ins.Get<IRepoZuhuaPort>();
            rzu.ReCreateTables("a");

            var e = new ZuhuaRecord()
            {
                发货单码 = "1",
                Id = 1,
            };
            e.addItem(new ZuhuaZiLingItem()
            {
                ZaHuaItemId = 3
            });
            //CRUD
            rzu.SaveAsync(e);

            var res = rzu.GetZuhuaRecordsByDay(DateTime.Today.AddDays(0));
            res.Echo();

            rzu.Del(e.Id);

            res = rzu.GetZuhuaRecordsByDay(DateTime.Today.AddDays(0));
            res.Count().Echo();
        }

        [Test]
        public void T2_zahua_Crid_presure()
        {
            // var en = AntiObject.Ins.Get<IRepoZahua>();
            var rzu = AntiObject.Ins.Get<IRepoZuhuaPort>();

            ulong n = 10;
            ulong nn = 30;

            for (ulong i = 0; i < n; i++)
            {
                var e = new ZuhuaRecord()
                {
                    发货单码 = "1",
                    Id = 1 + i * 100,
                };


                for (ulong j = 0; j < nn; j++)
                {
                    e.addItem(new ZuhuaZiLingItem()
                    {
                        ZaHuaItemId = 3 + j
                    });
                }

                //CRUD
                rzu.SaveAsync(e);
            }
        }


        [Test]
        public void T3_zahua_Crid_reset()
        {
            // var en = AntiObject.Ins.Get<IRepoZahua>();
            var zm = AntiObject.Ins.Get<IRepoZahua>();

            zm.ReCreateTables("a");
        }


        [Test]
        public async Task T3_zahua_save_reset()
        {
            var n = 1000;

            #region data

            // var en = AntiObject.Ins.Get<IRepoZahua>();
            var zm = AntiObject.Ins.Get<IRepoZahua>();
            zm.ReCreateTables("a");

            var ls = new List<ZaHuaItem>();
            for ( var  i = 0; i < n ; i++)
            {
                ls.Add(new ZaHuaItem()
                {
                    IdBackPointer =(ulong) i,
                });
            }
            

            #endregion
            var sw = 1.StartWatch();

            #region try

            //  Parallel.ForEach(ls, async l =>
            // {
            //     await zm.SaveEntityAsync(l);
            // });
            
            // foreach (var z in ls)
            // {
            //     await zm.SaveEntityAsync(z, false);
            //
            // }

            #endregion
            
            // await Task.WhenAll(  ls.Select(x => zm.SaveEntityAsync(x,false)) ) ;
            await zm.SaveEntitysAsync(ls);
            
            sw.Report("t");
            
            
        }

        [Test]
        public void T31_zahua_Crud_de()
        {
            var j = @"
{
  ""IsEmtpy"" : false,
  ""Id"" : 17165461902110208000,
  ""IdBackPointer"" : 3359463,
  ""扎花打码"" : ""009.04.45.10-T28-C08-DK40_01:P1K_0062"",
  ""供货商码"" : ""30340"",
  ""地块"" : ""DK40"",
  ""货品编码"" : ""HP40"",
  ""品种"" : ""009"",
  ""长度"" : ""45"",
  ""规格"" : ""10"",
  ""SKU"" : ""DK40_45_10_45677"",
  ""SKU别名"" : ""30340_45_10_45677"",
  ""供货日期"" : ""2025-01-20T13:34:01"",
  ""指令时间"" : null,
  ""GroupKey"" : ""DK40_009_45_45677"",
  ""ZhiLingItemId"" : 0,
  ""业务类型"" : null,
  ""Changed"" : true
}";


            var o = j.ToObject<ZaHuaItem>();

            o.Echo();
        }
    }
}